#include "rrecentfilemodel.h"
#include <QString>

RRecentFileModel::RRecentFileModel(REngine *engine) :
    m_engine(engine)
{
    QHash<int, QByteArray> roles;
    roles[KFileRoleID] = CRoleFileId.toAscii();
    roles[KFileRoleFileName] = CRoleFileName.toAscii();
    roles[KFileRoleLineCount] = CRoleLineCount.toAscii();
    roles[KFileRoleLastModifyTime] = CRoleLastModifyTime.toAscii();
    roles[KFileRoleLastReadTime] = CRoleLastReadTime.toAscii();
    roles[KFileRoleLastReadLine] = CRoleLastReadLine.toAscii();
    roles[KFileRoleLastReadCharIndex] = CRoleLastReadCharIndex.toAscii();
    setRoleNames(roles);
    m_engine->recentFileInfo(m_file_info_list);
}

int RRecentFileModel::count() const {
    return rowCount();
}

int RRecentFileModel::rowCount(const QModelIndex & /*parent*/) const {
    return m_file_info_list.count();
}

QVariant RRecentFileModel::data(const QModelIndex & index, int role) const {
    int row = index.row();
    switch (role) {
    case KFileRoleID:
        return m_file_info_list[row].id;

    case KFileRoleFileName:
        return m_file_info_list[row].name;

    case KFileRoleLineCount:
        return m_file_info_list[row].lineCount;

    case KFileRoleLastModifyTime:
        return m_file_info_list[row].lastModifyTime;

    case KFileRoleLastReadTime:
        return m_file_info_list[row].lastReadTime;

    case KFileRoleLastReadLine:
        return m_file_info_list[row].lastReadLine;

    case KFileRoleLastReadCharIndex:
        return m_file_info_list[row].lastReadCharIndex;

    default:
        return QVariant();
    }
}

void RRecentFileModel::reload() {
    beginResetModel();
    m_file_info_list.clear();
    m_engine->recentFileInfo(m_file_info_list);
    endResetModel();
}
